package com.ydcode.utils;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.Amf3Output;

/**
 * java 数据序列化
 * 
 * @author YUPENG.LI
 * @version 1.0.0.0
 * 
 */
public class JABeanUtil {

	public synchronized static void toAS3Bin(Object data, String filePath) {
		try {
			SerializationContext serializationContext = new SerializationContext();
			Amf3Output amfOut = new Amf3Output(serializationContext);
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			DataOutputStream dataOutStream = new DataOutputStream(outStream);
			amfOut.setOutputStream(dataOutStream);
			FileOutputStream os = new FileOutputStream(filePath);

			amfOut.writeObject(data);
			amfOut.flush();
			amfOut.close();

			os.write(outStream.toByteArray());
			os.flush();
			os.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void saveObject(Object obj, String path) {

		try {
			FileOutputStream fos = new FileOutputStream(path);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(obj);
			oos.flush();
			oos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Object loadObject(String path) {
		try {
			FileInputStream fis = new FileInputStream(path);
			ObjectInputStream ois = new ObjectInputStream(fis);

			return ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Java数据类型转换Flex的 <br>
	 * null : null <br>
	 * java.lang.Number : Number <br>
	 * java.lang.Number，boolean : Boolean <br>
	 * java.lang.String，java.lang.Character char[] : String <br>
	 * java.util.Collection，java.lang.reflect.Array : Array <br>
	 * java.util.Map，java.util.Dictionary : Object <br>
	 * org.w3c.dom.Document : XML Object <br>
	 * claess : Object
	 * 
	 * @param type
	 * @return
	 */
	public static String JavaToFlexType(String type) {

		if (type.equals("java.util.Date") || type.equals("java.sql.Timestamp")
				|| type.equals("java.sql.Date")) {
			return "Date";
		} else if (type.equals("java.lang.Integer")) {
			return "int";
		} else if (type.equals("java.lang.Number")
				|| type.equals("java.lang.Double")
				|| type.equals("java.lang.Long")) {
			return "Number";
		} else if (type.equals("java.lang.Boolean")) {
			return "Boolean";
		} else if (type.equals("java.lang.String")
				|| type.equals("java.lang.Character")) {
			return "String";
		} else if (type.equals("java.util.Collection")
				|| type.equals("java.lang.reflect.Array")) {
			return "Array";
		} else if (type.equals("java.util.Map")
				|| type.equals("java.util.Dictionary")) {
			return "Object";
		} else {
			return "Object";
		}
	}
	
	/**
	 * Java 转 Flex的数据 <br>
	 * null : null <br>
	 * java.lang.Number : Number <br>
	 * java.lang.Number，boolean : Boolean <br>
	 * java.lang.String，java.lang.Character char[] : String <br>
	 * java.util.Collection，java.lang.reflect.Array : Array <br>
	 * java.util.Map，java.util.Dictionary : Object <br>
	 * org.w3c.dom.Document : XML Object <br>
	 * claess : Object
	 * 
	 * @param type
	 * @return
	 */
	public static String FlexToJavaType(String type) {

		if (type.equals("java.util.Date") || type.equals("java.sql.Timestamp")
				|| type.equals("java.sql.Date")) {
			return "Date";
		} else if (type.equals("java.lang.Integer")) {
			return "int";
		} else if (type.equals("java.lang.Number")
				|| type.equals("java.lang.Double")
				|| type.equals("java.lang.Long")) {
			return "Number";
		} else if (type.equals("java.lang.Boolean")) {
			return "Boolean";
		} else if (type.equals("java.lang.String")
				|| type.equals("java.lang.Character")) {
			return "String";
		} else if (type.equals("java.util.Collection")
				|| type.equals("java.lang.reflect.Array")) {
			return "Array";
		} else if (type.equals("java.util.Map")
				|| type.equals("java.util.Dictionary")) {
			return "Object";
		} else {
			return "Object";
		}
	}
}